import 'package:flutter/material.dart';
import 'package:flutter_douban/config/Http_service.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/loading/custom_header.dart';
import 'package:flutter_douban/main_page/class/model/class_big_model.dart';
import 'package:flutter_douban/main_page/class/model/class_c_model.dart';
import 'package:flutter_douban/main_page/class/view/class_cell_view.dart';
import 'package:flutter_douban/tool_views/stance_page.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class ClassRoomCategorySonPage extends StatefulWidget {
  final int type;

  const ClassRoomCategorySonPage({Key? key,required this.type}) : super(key: key);

  @override
  _ClassRoomCategorySonPageState createState() => _ClassRoomCategorySonPageState();
}

class _ClassRoomCategorySonPageState extends State<ClassRoomCategorySonPage> with AutomaticKeepAliveClientMixin {

  /// 列表数据
  List<ClassCModel> _list = [];

  /// 控制器
  EasyRefreshController? _easyRefreshController;

  Future _getRecommendData() async {
    try {
      /// 全部数据
      Map<String, dynamic> result = await HttpService.getMockDataWithPath(
          path: widget.type == 1 ? 'mock/class_category_common.json' : 'mock/class_category_new.json');
      /// 轮播图数据
      ClassBigModel classBigModel = ClassBigModel.fromJson(result['result']);
      setState(() {
        _list = classBigModel.list!;
      });
      /// 结束加载状态
      _easyRefreshController!.resetLoadState();
      _easyRefreshController!.finishLoad(noMore: true);
    } catch (e) {} finally {}
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    /// 请求数据
    _getRecommendData();
  }

  @override
  Widget build(BuildContext context) {
    Widget nonalWidget;

    if (_list.length > 0) {
      nonalWidget = EasyRefresh(
          firstRefresh: false,
          header: CustomHeaderView(),
          controller: _easyRefreshController,
          enableControlFinishRefresh: false,
          enableControlFinishLoad: true,
          onRefresh: _getRecommendData,
          child: Container(
            color: AppColors.page,
            padding: EdgeInsets.only(left: 14, right: 14, top: 10),
            child: GridView.builder(
                itemCount: _list.length,
                primary: false,
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  //横轴元素个数
                    crossAxisCount: 2,
                    //纵轴间距
                    mainAxisSpacing: 1.0,
                    //横轴间距
                    crossAxisSpacing: 10.0,
                    //子组件宽高长度比例
                    childAspectRatio: 0.53),
                itemBuilder: (BuildContext context, int index) {
                  return ClassCellView(
                    model: _list[index],
                  );
                }),
          ));
    } else {
      nonalWidget = StancePage();
    }

    return Scaffold(
        // appBar: AppBar(
        //   title: NavTitlePage(titleStr: widget.titleStr),
        // ),
        body: nonalWidget);
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
